Dart SDK CompilerContext
CompilerContext
in the Dart SDK is a central mechanism for managing and maintaining the state and settings of the compiler across its various components and throughout its operation. Its design ensures consistency, efficient state management, and convenient access to shared compiler information, crucial for the compiler's efficient and error-free operation.
Shared Compiler Context: CompilerContext
is a shared context used throughout the compiler. This indicates that it serves as a centralized repository or environment for various aspects of the compiler's operation. It maintains state and settings that are common across different components of the compiler.
Single Instance in Use: The compiler operates with a single instance of this class, suggesting that CompilerContext
is designed following a singleton pattern within the scope of a compilation process. This ensures consistency in the compiler's behavior and state management.
Stored as a Dart Zone-Value: The context is stored as a zone-value rather than being passed around as an argument. In Dart, zones define an execution context, maintaining values across the asynchronous flow of the code. Storing CompilerContext
as a zone-value implies that it can maintain state and behavior consistently across asynchronous operations and different parts of the compiler.
Convenient Access through Static Getter: The class provides a static getter, [CompilerContext.current]
, which retrieves the context stored in the current zone. This design choice enhances the ease of access to the compiler context, allowing different parts of the compiler to conveniently and consistently access shared state and configurations.
Role in Compiler Operation: Given these characteristics, CompilerContext
plays a crucial role in the Dart compiler's operation. It acts as a backbone for managing shared state, configurations, and information flow within the compiler, facilitating the coordination and communication between various compiler components.
Zone-value
Instance creation: The static function runWithOptions
will create a new CompilerContext
instance.
When does CompilerContext
instance be put into the Zone: in the implementation of runWithOptions, when CompilerContext
is created, runInContext will be called:
static Future<T> runWithOptions<T>(
ProcessedOptions options, Future<T> action(CompilerContext c),
{bool errorOnMissingInput = true}) {
return new CompilerContext(options)
.runInContext<T>((CompilerContext c) async {
await options.validateOptions(errorOnMissingInput: errorOnMissingInput);
return action(c);
});
}
The runInContext
function is actually a wrapper of runZoned
, and put the instance of CompilerContext
into this Zone.
When the uriToSource
is added?
The uriToSource
is a Map, its signature:
final Map<Uri, Source> uriToSource = <Uri, Source>{};
If we search through the soruce code of the Dart SDK, we cannot find the directly modification of uriToSource
. (Actually I find one occurance.)
The uriToSource
is used as a sharing way, for example, In the KernelTarget:
final Map<Uri, Source> uriToSource = CompilerContext.current.uriToSource;
From the code, we can see the uriToSource
of KernelTarget and the one in CompilerContext are the same instance.
本文作者:Maeiee
版权声明:如无特别声明,本文即为原创文章,版权归 Maeiee 所有,未经允许不得转载!
喜欢我文章的朋友请随缘打赏,鼓励我创作更多更好的作品!